var http = require("http"),
	url = require("url"),
	path = require("path"),
	fs = require("fs");
var querystring = require("querystring");
//	var redis = require('redis');  
//处理文件分发
http.createServer(function(req, res) {
	
	
	
	if(req.url.indexOf('/api') >= 0) {
		console.log(req.url)
		return;
	}
	if(req.url == '/file') {//上传接口
		console.log('文件上传');
		// 设置接收数据编码格式为 UTF-8  
		req.setEncoding('utf-8');
		var postData = ""; //POST & GET ： name=zzl&email=zzl@sina.com  
		// 数据块接收中  
		req.addListener("data", function(postDataChunk) {
			postData += postDataChunk;
		});
		// 数据接收完毕，执行回调函数  
		req.addListener("end", function() {
		var params = querystring.parse(postData); //GET & POST  ////解释表单数据部分{name="zzl",email="zzl@sina.com"}  
			var params = postData;
			var baseArray = params.split(',');
			var baseStr = baseArray[1]
			console.log('文件头--' + baseArray[0])
			var suffix = baseArray[0].split(';');
			var suffix = suffix[0].split('/');
			var suffix = suffix[1];
			console.log(suffix);
			var filePath = 'file/' + (new Date().getTime()) + '.' + suffix
			base64_decode(baseStr, filePath)
			res.writeHead(200, {
				"Content-Type": "text/plain;charset=utf-8"
			});
			res.end(filePath);
		});
		return
	}
	console.log('req.url' + req.url)
	var pathname = __dirname + url.parse(req.url).pathname;
	console.log('请求pathname---' + pathname)
	if(path.extname(pathname) == "") {
		pathname += "/";
	}
	if(pathname.charAt(pathname.length - 1) == "/") {
		pathname += "index.html";
	}

	if(path.isAbsolute(pathname)) {
		console.log('请求path---' + path.extname(pathname))
		switch(path.extname(pathname)) {
			case ".html":
				res.writeHead(200, {
					"Content-Type": "text/html"
				});
				break;
			case ".js":
				res.writeHead(200, {
					"Content-Type": "text/javascript"
				});
				break;
			case ".css":
				res.writeHead(200, {
					"Content-Type": "text/css"
				});
				break;
			case ".gif":
				res.writeHead(200, {
					"Content-Type": "image/gif"
				});
				break;
			case ".jpg":
				res.writeHead(200, {
					"Content-Type": "image/jpeg"
				});
				break;
			case ".png":
				res.writeHead(200, {
					"Content-Type": "image/png"
				});
				break;
			default:
				res.writeHead(200, {
					"Content-Type": "application/octet-stream"
				});
		}

		fs.readFile(pathname, function(err, data) {
			res.end(data);
		});
	} else {
		res.writeHead(404, {
			"Content-Type": "text/html"
		});
		res.end("<h1>404 Not Found</h1>");
	}

}).listen(8124);

console.log("Server running at http://127.0.0.1:8124/");
//表单接收完成后，再处理redis部分  
function PushToRedis(info) {
	var client = redis.createClient();
	client.lpush("topnews", info);
	console.log("PushToRedis:" + info);
	client.lpop("topnews", function(i, o) {
		console.log(o); //回调，所以info可能没法得到o的值，就被res.write输出了  
	})
	client.quit();
}

// function to create file from base64 encoded string
function base64_decode(base64str, file) {
	// create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded
	var bitmap = new Buffer(base64str, 'base64');
	// write buffer to file
	fs.writeFileSync(file, bitmap);
}